package com.shuda.system.controller;

import com.shuda.common.dto.PageDTO;
import com.shuda.common.dto.PageResult;
import com.shuda.common.result.Result;
import com.shuda.system.dto.MenuDTO;
import com.shuda.system.dto.MenuQueryDTO;
import com.shuda.system.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 菜单管理控制器
 */
@RestController
@RequestMapping("/api/system/menu")
@Tag(name = "菜单管理", description = "菜单管理相关接口")
@RequiredArgsConstructor
@Validated
public class MenuController {

    private final MenuService menuService;

    /**
     * 获取菜单分页列表
     */
    @PostMapping("/page")
    @Operation(summary = "获取菜单分页列表")
    public Result<PageResult<MenuDTO>> getMenuPage(@Valid @RequestBody PageDTO pageDTO, MenuQueryDTO queryDTO) {
        PageResult<MenuDTO> result = menuService.getMenuPage(pageDTO, queryDTO);
        return Result.success(result);
    }

    /**
     * 根据ID获取菜单详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取菜单详情")
    public Result<MenuDTO> getMenuById(@PathVariable Long id) {
        MenuDTO menuDTO = menuService.getMenuById(id);
        return Result.success(menuDTO);
    }

    /**
     * 创建菜单
     */
    @PostMapping
    @Operation(summary = "创建菜单")
    public Result<Long> createMenu(@Valid @RequestBody MenuDTO menuDTO) {
        Long id = menuService.createMenu(menuDTO);
        return Result.success(id);
    }

    /**
     * 更新菜单
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新菜单")
    public Result<String> updateMenu(@PathVariable Long id, @Valid @RequestBody MenuDTO menuDTO) {
        menuService.updateMenu(id, menuDTO);
        return Result.success("菜单更新成功");
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除菜单")
    public Result<String> deleteMenu(@PathVariable Long id) {
        menuService.deleteMenu(id);
        return Result.success("菜单删除成功");
    }

    /**
     * 批量删除菜单
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除菜单")
    public Result<String> batchDeleteMenu(@RequestBody List<Long> ids) {
        menuService.batchDeleteMenu(ids);
        return Result.success("菜单批量删除成功");
    }

    /**
     * 更新菜单状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新菜单状态")
    public Result<String> updateMenuStatus(@PathVariable Long id, @RequestParam Integer status) {
        menuService.updateMenuStatus(id, status);
        return Result.success("菜单状态更新成功");
    }

    /**
     * 获取所有启用的菜单列表
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有启用的菜单列表")
    public Result<List<MenuDTO>> getAllEnabledMenus() {
        List<MenuDTO> menus = menuService.getAllEnabledMenus();
        return Result.success(menus);
    }

    /**
     * 获取菜单树结构
     */
    @GetMapping("/tree")
    @Operation(summary = "获取菜单树结构")
    public Result<List<MenuDTO>> getMenuTree() {
        List<MenuDTO> menuTree = menuService.getMenuTree();
        return Result.success(menuTree);
    }

    /**
     * 获取用户菜单列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户菜单列表")
    public Result<List<MenuDTO>> getUserMenus(@PathVariable Long userId) {
        List<MenuDTO> menus = menuService.getUserMenus(userId);
        return Result.success(menus);
    }
}